/**
 * @tag controllers, home
 */
jQuery.Controller.extend('Bugstat.Controllers.ImportantDates',
/* @Static */
{
},
/* @Prototype */
{
	importantDates : 0,
	successLoadCallback : 0,
	errorLoadCallback : 0,
	
	
	/**
	 * Load avilable important dates.
	 *
	 * @param {Function} success  A callback function that is called if loading succeded.
     * @param {Function} error   A callback function for an error in the ajax request.     
	 */ 
    loadDates: function( success, error ) 
	{
		this.successLoadCallback = success;
		this.errorLoadCallback = error;
		
		Bugstat.Models.ImportantDates.findAll({}, this.callback('handleImportantDatesLoaded'), this.callback('handleErrorLoading') ) ;		
	},
	
	/**
	 * Attach dates to a given placeholder tag (that has given plot)
	 *
	 * @param {Object} placeholder  Tag object to attach dates to
     * @param {Object} plot         Flot plot object
	 */
	attachDatesToPlot : function (placeholder, plot)
	{
		placeholder.append('//bugstat/views/report/important_dates.ejs', {plot: plot, dates: this.importantDates.dates } );
	},
	
	
	/**
	 * Update plot options with variables nessesary for nice important
	 * date display
	 *
	 * @param {Object} plotOptions  Options to udpate
	 */
	updatePlotOptions: function( plotOptions )
	{
		if ( this.importantDates.dates.length > 0 )
		{
			plotOptions.grid.drawVertical = false;
			plotOptions.grid.drawHorizontal = false;			
		}
		else
		{
			plotOptions.grid.drawVertical = true;
			plotOptions.grid.drawHorizontal = true;						
		}
	},
	
	/**
	 * Save loaded dates.
	 */	
	handleImportantDatesLoaded: function( importantDates )
	{
		if ( importantDates )
		{
			this.importantDates = importantDates;
			this.importantDates.fixTimestamps();

			if ( this.successLoadCallback ) this.successLoadCallback();
		}
		else
		{
			this.handleErrorLoading( 0, 0, 'cant_load_important_dates' );
		}
	},
	
	handleErrorLoading: function(request, error, status) 
	{
		if ( this.errorLoadCallback ) this.errorLoadCallback( request, error, status );
	},
});